package onlyTest;

import com.wechat.order.util.excelUtils.ExcelProSetUtil;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * Created by zzy on 2017/12/30.
 */
public class createTemplate {
    public static void main(String[] args) {
      String parentPath=  System.getProperty("user.dir");
      String path=parentPath+"\\src\\main\\resources\\xml\\student2.xml";
        File file=new File(path);
        SAXBuilder builder=new SAXBuilder();
        try {
            //解析xml
            Document parse= builder.build(file);
            //创建excel
            HSSFWorkbook workbook=new HSSFWorkbook();
            HSSFSheet sheet=workbook.createSheet("Sheet0");

            //获取xml文件根节点
            Element root=parse.getRootElement();
            //获取模板名称
            String templateName=root.getAttribute("name").getValue();

            int rownum=0;
            int columnnum=0;
            //设置列宽
            Element colgroup=root.getChild("colgroup");
            ExcelProSetUtil.setColumnWidth(sheet,colgroup);
            //设置标题
            Element title=root.getChild("title");
            ExcelProSetUtil.setSheetTitle(workbook,sheet,title);
            rownum++;
            //设置表头
            Element thead=root.getChild("thead");
            ExcelProSetUtil.setSheetThead(sheet,thead);
            rownum++;
            //设置数据区域属性
            Element tbody=root.getChild("tbody");
            ExcelProSetUtil.setSheetTbody(workbook,sheet,tbody);

            //生成excel导入模板
            File tempFile=new File("E:/"+templateName+"2.xls");
            tempFile.delete();
            tempFile.createNewFile();
            FileOutputStream stream= FileUtils.openOutputStream(tempFile);
            workbook.write(stream);
            stream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(path);
    }
}
